const express = require('express');
const router = express.Router();

// 导入工具和中间件
const { success, error, badRequest, asyncHandler, paginated } = require('../utils/response');
const { optionalAuth, authenticateToken } = require('../utils/auth');

// 导入服务层
const studyService = require('../services/studyService');

/**
 * 获取课程分类
 * GET /study/categories
 */
router.get('/categories', asyncHandler(async (req, res) => {
  const categories = await studyService.getCourseCategories();
  success(res, categories);
}));

/**
 * 获取推荐课程
 * GET /study/courses/recommended
 */
router.get('/courses/recommended', optionalAuth, asyncHandler(async (req, res) => {
  try {
    const userId = req.user ? req.user.id : null;
    const result = await studyService.getRecommendedCourses(req.query, userId);
    paginated(res, result.list, result.total, result.page, result.limit);
  } catch (err) {
    return badRequest(res, err.message);
  }
}));

/**
 * 搜索课程
 * GET /study/courses/search
 */
router.get('/courses/search', optionalAuth, asyncHandler(async (req, res) => {
  try {
    const { keyword } = req.query;
    if (!keyword || keyword.trim().length === 0) {
      return badRequest(res, '搜索关键词不能为空');
    }

    const userId = req.user ? req.user.id : null;
    const result = await studyService.searchCourses(req.query, userId);
    paginated(res, result.list, result.total, result.page, result.limit);
  } catch (err) {
    return badRequest(res, err.message);
  }
}));

/**
 * 获取课程详情
 * GET /study/courses/:id
 */
router.get('/courses/:id', optionalAuth, asyncHandler(async (req, res) => {
  const { id } = req.params;

  if (!id || isNaN(parseInt(id))) {
    return badRequest(res, '无效的课程ID');
  }

  try {
    const userId = req.user ? req.user.id : null;
    const courseDetail = await studyService.getCourseDetail(parseInt(id), userId);
    success(res, courseDetail);
  } catch (err) {
    return badRequest(res, err.message);
  }
}));

/**
 * 报名课程
 * POST /study/courses/:id/enroll
 */
router.post('/courses/:id/enroll', authenticateToken, asyncHandler(async (req, res) => {
  const { id } = req.params;

  if (!id || isNaN(parseInt(id))) {
    return badRequest(res, '无效的课程ID');
  }

  try {
    const result = await studyService.enrollCourse(req.user.id, parseInt(id));
    success(res, result, '报名成功');
  } catch (err) {
    return badRequest(res, err.message);
  }
}));

/**
 * 更新学习进度
 * PUT /study/courses/:id/progress
 */
router.put('/courses/:id/progress', authenticateToken, asyncHandler(async (req, res) => {
  const { id } = req.params;

  if (!id || isNaN(parseInt(id))) {
    return badRequest(res, '无效的课程ID');
  }

  try {
    const result = await studyService.updateCourseProgress(req.user.id, parseInt(id), req.body);
    success(res, result, '进度更新成功');
  } catch (err) {
    return badRequest(res, err.message);
  }
}));

/**
 * 获取我的课程
 * GET /study/my-courses
 */
router.get('/my-courses', authenticateToken, asyncHandler(async (req, res) => {
  const result = await studyService.getUserCourses(req.user.id, req.query);
  paginated(res, result.list, result.total, result.page, result.limit);
}));

module.exports = router;
